import json
import uuid

from loguru import logger

from CloudEngine.IO.ICgnsReader import ICgnsReader
from CloudEngine.IO.ICgnsReaderV4 import ICgnsReaderV4
from CloudEngine.IO.StandardJsonLoader import StandardJsonLoader
from CloudEngine.core.StandardActor import StandardActor
from service.CacheSystem import check_cache, load_cache, save_cache
from service.DataCenter import db
from service.utils.download_file import download_file

from pathlib import Path

from service.utils.file import get_file_md5, read_file, write_file
from service.utils.scene_helper import release_actor_to_scene, get_surface


def read_cgns(service, params) -> str:
    """
    读取CGNS文件并将其添加到服务中。

    参数:
    service (Service): 服务对象，用于添加读取的CGNS文件。
    params (dict): 包含CGNS文件路径的参数字典。

    返回:
    str: 添加到服务中的CGNS文件的ID。
    """
    # 获取参数中的路径
    path = params.get("path")
    # 生成一个唯一的文件名
    entry_file = str(uuid.uuid4()) + ".cgns"
    # 本地路径
    local_path = "./tmp/" + entry_file
    # 下载文件到本地
    # http文件地址
    if path.find("http") != -1:
        download_file(path, local_path)
    else:
        db.S3.download_file(path, local_path)

    md5 = get_file_md5(local_path)
    standard_data = None
    if check_cache(md5):
        standard_file_path = load_cache(md5)
        standard_data = json.loads(read_file(standard_file_path))
        logger.success("命中缓存")
    # 判断cgns 版本
    V4_Reader = ICgnsReaderV4()
    version = float(V4_Reader.get_version(local_path)[0])
    if version >= 4:

        if standard_data is None:
            V4_Reader.read(local_path)
            standard_data = V4_Reader.standard
            tmp_path = "./tmp/cache_" + md5 + ".json"
            write_file(tmp_path, json.dumps(standard_data))
            save_cache(md5, tmp_path)

        loader = StandardJsonLoader()
        loader.parse(standard_data)

        ac = None
        if not loader.has_d2:
            ac = StandardActor(get_surface(loader.d3_poly_data))
        else:
            ac = StandardActor(loader.poly_data)
        ac.attributes = standard_data["attributes"]
        ac.loader = loader
        ac.set_render_type("SURFACE_WITH_EDGE")
        ac.name = Path(path).name
        ac.topology = standard_data["blocks"]
        service.model_add(ac)
        release_actor_to_scene(service, loader, ac)
        return ac.id
    else:
        if standard_data is None:
            reader = ICgnsReader()
            reader.read(local_path)
            standard_data = reader.to_standard_json_structure()
            tmp_path = "./tmp/cache_" + md5 + ".json"
            write_file(tmp_path, json.dumps(standard_data))
            save_cache(md5, tmp_path)

        loader = StandardJsonLoader()
        loader.parse(standard_data)
        ac = StandardActor(loader.poly_data)
        ac.attributes = standard_data["attributes"]
        ac.topology = standard_data["blocks"]
        ac.loader = loader
        ac.set_render_type("SURFACE_WITH_EDGE")
        ac.name = Path(path).name
        service.model_add(ac)
        return ac.id
